$(document).ready(function(){
	$('.settime').css('left', ($('body').width() - $('.settime').width()) / 2);
	//查询指定url参数
	function GetQueryString(name) {
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
	}
	var id = GetQueryString("id");
	var uuid = GetQueryString("uuid");
	var obj;
	$.ajax({
        url : "/app/goods/detail.api",
        type : "post",
        data:{
          "id":id
        },
        beforeSend: function(xhr){
        	xhr.setRequestHeader('uuid', uuid);
        },
        success : function(datas){
 			loadGoods();
            obj = datas.data;
            $(".productName").html(obj.goodsName);
            $(".productSell").html(obj.salePrice);
            if(obj.picture != null) {
            	$(".banner img").attr("src",obj.picture);
            }
 			if(obj.collect == '1') {
 				$('#productLove').attr('src', 'img/collectselect.png');
 			}
            $(".settime").attr("endTime", obj.limit.endTime);
 			updateEndTime();
        }
    });
  var pageNum = 1;
  function loadGoods(){
	  $.ajax({
	            url : "/app/goods/listlimit.api",
	            type : "post",
	            data:{
	              pageIndex: pageNum,
	              isLimit: '1'
	            },
	            success : function(datas){
		            var obj = datas.rows || [];
		            if(obj==null || obj.length == 0){
	  
		            }else{  
		               for(var i=0;i<obj.length;i++){
		               		var goodsLimit = obj[i];
		               		var attributes ="&nbsp;";
		               		if(goodsLimit.goods.attributes!=null && goodsLimit.goods.attributes.length!=0){
		               			attributes = goodsLimit.goods.attributes[0].attributeName+'：'+goodsLimit.goods.attributes[0].attributeValue;
		               		}
							var html = ' <div class="store">'
								+'<div class="storeTitle" >'
									+'<div style="float: left; width: 60%">'
									+'	<span>&nbsp;&nbsp;'
										+'	<img src="' + goodsLimit.goods.store.storeIcon + '" height="17px" style="vertical-align:middle" />'
										+'</span> '
										+'<span class="storeName">'+goodsLimit.goods.store.storeName+'</span>'
									+'</div>'
									+'<span class="storeDeliver">配送费:<span class="storeDeliverFee">0.00</span>元&nbsp;&nbsp;</span>'
								+'</div>'
								+'<div class="line"></div>'
								+'	<div class="storeInfo">'
								+'		<img src="'+goodsLimit.goods.picture+'">'
								+'		<div class="storeInfoTxt">'
								+'			<div class="storeProductName">'+goodsLimit.goods.goodsName+'</div>'
								+'			<div class="attr">'+attributes+'</div>'
								+'			<div class="gray"><s>原价:'+goodsLimit.goods.goodsPrice+'</s></div>'
								+'         <span class="gray">折扣价:<span class="sell">￥</span><span class="productSell">'+goodsLimit.goods.salePrice+'</span></span>'
								+'			<span class="StoreFlashSale" onclick="buy('+goodsLimit.goods.id+','+goodsLimit.goods.storeId+')">&nbsp;立即抢购&nbsp;</span>'
								+'		</div>'
								+'</div>'
							+'</div>    ';
							$("body").append(html);
		               }
		            }
	            }
	        });
		}
 	 //--------------上拉加载更多---------------
       //获取滚动条当前的位置 
       function getScrollTop() {
           var scrollTop = 0;
           if(document.documentElement && document.documentElement.scrollTop) {
               scrollTop = document.documentElement.scrollTop;
           } else if(document.body) {
               scrollTop = document.body.scrollTop;
           }
            return scrollTop;
        }

        //获取当前可视范围的高度 
        function getClientHeight() {
            var clientHeight = 0;
            if(document.body.clientHeight && document.documentElement.clientHeight) {
                clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
            } else {
                clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
            }
            return clientHeight;
        }

        //获取文档完整的高度 
        function getScrollHeight() {
            return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
        }
        
        //滚动事件触发
        window.onscroll = function() {
            if(getScrollTop() + getClientHeight() == getScrollHeight()) {
            	pageNum++;
            	loadGoods();
            }
        }

        //-----------------结束--------------------    
    	//购买点击事件
    	$(".btnFlashSale").click(function(){
    		buy(id, obj.storeId);
    	});
     $('.productLove').on('click', function() {
    	 $.ajax({
    	        url : "/app/member/addCollection.api",
    	        type : "post",
    	        data:{
    	          collectionType: '0',
    	          collectionId: id
    	        },
    	        beforeSend: function(xhr){
    	        	xhr.setRequestHeader('uuid', uuid);
    	        },
    	        success : function(datas){
    	        	if(datas.code=='401') {
    	        		window.location.href='http://xshop/doAction?param={"type":"login","goodsId":"'+id+'","storeId":"'+obj.storeId+'"}';
    	        	} else if(datas.collect == '1') {
    	        		$('#productLove').attr('src', 'img/collectselect.png');
    	        	} else {
    	        		$('#productLove').attr('src', 'img/collect.png');
    	        	}
    	        }
    	 });
     });
     $('.productShare').on('click', function() {
           window.location.href='http://xshop/doAction?param={"type":"share","goodsId":"'+id+'","storeId":"'+obj.storeId+'","goodsName":"'+obj.goodsName+'"}';
     });
});
//倒计时函数
function updateEndTime() {
    var date = new Date();
    var time = date.getTime(); //当前时间距1970年1月1日之间的毫秒数
    $(".settime").each(function(i){
	    var endDate =this.getAttribute("endTime"); //结束时间字符串
	    //转换为时间日期类型
	    var endDate1 = eval('new Date(' + endDate.replace(/\d+(?=-[^-]+$)/, function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) +')');
	    var endTime = endDate1.getTime(); //结束时间毫秒数
	    var lag = (endTime - time) / 1000; //当前时间和结束时间之间的秒数
	    if(lag > 0) {
		    var second = Math.floor(lag % 60); 
		    var minite = Math.floor((lag / 60) % 60);
		    var hour = Math.floor((lag / 3600) % 24);
		    var day = Math.floor((lag / 3600) / 24);
		    $(this).html("还剩 <b>"+day+"</b> 天 <b>"+hour+"</b> 时 <b>"+minite+"</b> 分 <b>"+second+"</b> 秒");
	    } else {
	      $(this).html("已经结束啦！");
	    }
    });
    setTimeout("updateEndTime()",1000);
}
function buy(id,storeId){
    window.location.href='http://xshop/doAction?param={"type":"buy","goodsId":"'+id+'","storeId":"'+storeId+'"}'
}